using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using Microsoft.SharePoint.Utilities;

namespace IWCommunity.Common
{
    public static class Logger
    {


        public static void LogError(string errorMessage, string errorCategory)
        {
            // log message - read this with tool like debug view from sysinternals
            Debug.WriteLine("IWCommunity - " + Assembly.GetExecutingAssembly().FullName + ": " + errorCategory, errorMessage);
            
        }

        public static void LogError(Exception ex, string ErrorMessage)
        {
            // create error message
            string errorMessage = ErrorMessage + " " + ex.Message + " " + ex.StackTrace;

            // add any inner exceptions
            Exception innerException = ex.InnerException;
            while (innerException != null)
            {
                errorMessage += "Inner Error: " + innerException.Message + " " + innerException.StackTrace;
                innerException = innerException.InnerException;
            }

            // log error
            LogError(errorMessage, ex.GetType().ToString());

            //TODO: add logic to add to event log
            EventLog evt = new EventLog("Application", ".", "IWCommunity");
            evt.WriteEntry(Assembly.GetExecutingAssembly().FullName + " : " + errorMessage, System.Diagnostics.EventLogEntryType.Error, 10023);
        }

        public static void LogError(Exception ex)
        {
            LogError(ex, "");
        }

        public static void LogError(Exception ex, bool transferToErrorPage)
        {
            LogError(ex,"");
            SPUtility.TransferToErrorPage(ex.Message);
        }
    }
}
